package com.uliky.shouyiLibrary.order.web;

import com.uliky.common.utils.PageUtils;
import com.uliky.shouyiLibrary.order.service.OrderService;
import com.uliky.shouyiLibrary.order.vo.OrderConfirmVo;
import com.uliky.shouyiLibrary.order.vo.OrderSubmitVo;
import com.uliky.shouyiLibrary.order.vo.SubmitOrderResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Map;
import java.util.concurrent.ExecutionException;

@Controller
public class OrderWebController {
    @Autowired
    private OrderService orderService;
    @GetMapping("/toTrade")
    public String toTrade(Model model) throws ExecutionException, InterruptedException {
        //查询订单信息
        OrderConfirmVo orderConfirmVo = orderService.confirmOrder();
        model.addAttribute("orderConfirmData",orderConfirmVo);
        return "confirm";
    }
    @PostMapping(value = "/submitOrder")
    public String submitOrder( OrderSubmitVo orderSubmitVo , Model model, RedirectAttributes redirectAttributes) {
        SubmitOrderResponseVo vo  =orderService.submitOrder(orderSubmitVo);
        if(vo.getCode()==0) {
            //下单成功
            model.addAttribute("submitOrderResp",vo);
            return "pay";
        }else{
            String msg = "下单失败;";
            switch (vo.getCode()) {
                case 1: msg +="订单信息已过期，请重新提交";
                break;
                case 2: msg +="订单商品价格发生变化，请确认后再提交";
                    break;
                case 3: msg +="商品库存不足";
                    break;
            }
            redirectAttributes.addFlashAttribute("msg",msg);
            return "redirect:http://order.shouyiLibrary.com/toTrade";
        }
    }

}
